package com.lelouch.service;

import com.google.common.base.Preconditions;
import com.lelouch.bean.SysAcl;
import com.lelouch.bean.SysUser;
import com.lelouch.beans.PageQuery;
import com.lelouch.beans.PageResult;
import com.lelouch.common.RequestHolder;
import com.lelouch.dao.SysAclMapper;
import com.lelouch.dao.SysUserMapper;
import com.lelouch.exception.ParamException;
import com.lelouch.param.AclParam;
import com.lelouch.param.UserParam;
import com.lelouch.util.BeanValidator;
import com.lelouch.util.IpUtil;
import com.lelouch.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 权限点服务
 *
 * @author lcy
 * @create 2018-06-12 15:08
 */
@Service
public class SysAclService {

    @Autowired
    private SysAclMapper sysAclMapper;
    @Autowired
    private SysLogService sysLogService;


    public void save(AclParam aclParam) {
        //检查参数是否合法
        BeanValidator.check(aclParam);
        if (checkExist(aclParam.getAclModuleId(), aclParam.getName(), aclParam.getId())) {
            throw new ParamException("当前层级下已经有同名的权限点名称，请重新命名！");
        }

        SysAcl sysAcl = SysAcl.builder().name(aclParam.getName()).aclModuleId(aclParam.getAclModuleId()).url(aclParam.getUrl()).type(aclParam.getType())
                .status(aclParam.getStatus()).seq(aclParam.getSeq()).remark(aclParam.getRemark()).build();
        String code = generateCode();

        sysAcl.setCode(code);
        sysAcl.setOperator(RequestHolder.getUser().getUsername());
        sysAcl.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getRequest()));
        sysAcl.setOperateTime(new Date());
        sysAclMapper.insertSelective(sysAcl);
        sysLogService.saveAclLog(null,sysAcl);

    }

    private boolean checkExist(Integer aclModuleId, String name, Integer id) {
        return sysAclMapper.countByNameAndAclModuleId(aclModuleId, name, id) > 0;
    }

    public void update(AclParam aclParam) {
        //检查参数是否合法
        BeanValidator.check(aclParam);
        if (checkExist(aclParam.getAclModuleId(), aclParam.getName(), aclParam.getId())) {
            throw new ParamException("当前层级下已经有同名的权限点名称，请重新命名！");
        }
        SysAcl before = sysAclMapper.selectByPrimaryKey(aclParam.getId());
        Preconditions.checkNotNull(before, "待更新的权限点不存在");
        SysAcl after = SysAcl.builder().id(aclParam.getId()).name(aclParam.getName()).aclModuleId(aclParam.getAclModuleId()).url(aclParam.getUrl()).type(aclParam.getType())
                .status(aclParam.getStatus()).seq(aclParam.getSeq()).remark(aclParam.getRemark()).build();
        after.setOperator(RequestHolder.getUser().getUsername());
        after.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getRequest()));
        after.setOperateTime(new Date());
        sysAclMapper.updateByPrimaryKeySelective(after);
        sysLogService.saveAclLog(before,after);
    }






    public PageResult<SysAcl> pageByAclModuleId(Integer aclModuleId, PageQuery pageQuery){
        BeanValidator.check(pageQuery);
        int count = sysAclMapper.getAclCountByAclModuleId(aclModuleId);
        if (count > 0) {
            List<SysAcl> list = sysAclMapper.pageByAclModuleId(aclModuleId,pageQuery);
            return PageResult.<SysAcl>builder().total(count).data(list).build();
        }
        return PageResult.<SysAcl>builder().build();
    }

    public String generateCode(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return simpleDateFormat.format(new Date())+"_"+ + (int)(Math.random() * 100);
    }

}
